package com.example.studentapp.login;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.studentapp.MainActivity;
import com.example.studentapp.R;
import com.example.studentapp.db.SQLiteDB;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    TextView login_lanTV,login_new_userTV,login_get_pwdTV;
    Button loginBtn;
    EditText login_phoneET,login_pwdET;
    SharedPreferences sp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
    }

    private void init() {
        login_lanTV = findViewById(R.id.login_lanTV);
        login_new_userTV = findViewById(R.id.login_new_userTV);
        login_get_pwdTV  =  findViewById(R.id.login_get_pwdTV);
        loginBtn = findViewById(R.id.loginBtn);
        login_phoneET = findViewById(R.id.login_phoneET);
        login_pwdET = findViewById(R.id.login_pwdET);
        login_lanTV.setOnClickListener(this);
        login_new_userTV.setOnClickListener(this);
        login_get_pwdTV.setOnClickListener(this);
        loginBtn.setOnClickListener(this);
        sp = getSharedPreferences("stuass", Context.MODE_PRIVATE);
        // 是否已经登录 已经登录跳转到主页面
        if(sp.getBoolean("login",false)){
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();

        }

    }


    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()){
            case R.id.login_lanTV:
                intent = new Intent(LoginActivity.this,LanActivity.class);
                startActivity(intent);
                break;
            case R.id.login_new_userTV:
                intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.login_get_pwdTV:
                intent = new Intent(LoginActivity.this,GetPwdActivity.class);
                startActivity(intent);
                break;
            case R.id.loginBtn:
                String phone = login_phoneET.getText().toString();
                String pwd = login_pwdET.getText().toString();
                Log.i("phone",phone);
                Log.i("pwd",pwd);
                if (phone.length() > 0 && pwd.length() > 0) {
                    SQLiteDB sqLiteDB = new SQLiteDB(getApplicationContext());
                    SQLiteDatabase sqLiteDatabase = sqLiteDB.getWritableDatabase();
                    String sql = "select * from user_info where phone='" + phone + "' and pwd='" + pwd + "'";
                    Cursor cursor = sqLiteDatabase.rawQuery(sql, null);
                    Log.w("Tag", String.valueOf(cursor.getCount()));
                    if (cursor.getCount() > 0) {
                        cursor.moveToFirst();
                        @SuppressLint("Range") int id=cursor.getInt(cursor.getColumnIndex("id"));
                        @SuppressLint("Range") String nickname=cursor.getString(cursor.getColumnIndex("nickname"));
                        @SuppressLint("Range") String reg_date=cursor.getString(cursor.getColumnIndex("reg_date"));
                        @SuppressLint("Range") String head=cursor.getString(cursor.getColumnIndex("head"));

                        intent = new Intent(LoginActivity.this, MainActivity.class);
                        sp.edit().putBoolean("login",true).putString("phone",phone).putInt("id",id)
                                .putString("nickname",nickname).putString("pwd",pwd).putString("reg_date",reg_date)
                                .putString("head",head).commit();
                        startActivity(intent);
                    } else
                        Toast.makeText(getApplicationContext(), getString(R.string.login_toast_error), Toast.LENGTH_SHORT).show();

                } else
                    Toast.makeText(getApplicationContext(), getString(R.string.login_toast_null), Toast.LENGTH_SHORT).show();
                break;

        }

    }
}